﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using AjaxControlToolkit;
using System.Drawing;

public partial class UserControl_MyIndexControl : System.Web.UI.UserControl
{
    private int maDanhMucHienDauTien;

    public int MaDanhMucHienDauTien
    {
        get { return maDanhMucHienDauTien; }
        set { 
            maDanhMucHienDauTien = value; 
         
        }
    }
   
    private string tenDanhMuc;

    public string TenDanhMuc
    {
        get { return tenDanhMuc; }
        set
        {
            tenDanhMuc = value;
            lblTenDanhMuc.Text = "Danh Mục:" + tenDanhMuc;
        }

    }

    public void setTextandLinkDanhMucChinh(string text, string link)
    {
        this.TenDanhMuc = text;
        lblTenDanhMuc.NavigateUrl = link;
    }

    private AdRotator quangCao;

    public AdRotator QuangCao
    {
        get { return quangCao; }
        set { 
            quangCao = value;
           // AdRotator1 = quangCao;
        }
    }

    public void setLinkQuangCao(string link)
    {
        //quangCao.AdvertisementFile = link;
       // AdRotator1.AdvertisementFile = link;
    }


    protected void Page_Load(object sender, EventArgs e)
    {
        
       
    }

    public void addDanhMucCon(int MaDanhMucCon, string TenDanhMucCon)
    {
        Button btn = new Button();
        btn.Text = TenDanhMucCon;
        btn.BackColor = Color.FromName("#7D7D7D");
        btn.BackColor = Color.FromArgb(56, 56, 56);
        btn.ForeColor = Color.WhiteSmoke;
        btn.Font.Bold = true;
        btn.Width = 80;
        btn.Height = 25;
      

       
        btn.ID = MaDanhMucCon.ToString();

        btn.Click += new EventHandler(btn_Click);
     
        pnlButton.Controls.Add(btn);
       
        

    }

    public void btn_Click(object sender, EventArgs e)
    {
        Button btn = (Button)sender;

       
        for (int i = 0; i < pnlButton.Controls.Count; i++ )
        {
            if(pnlButton.Controls[i] is Button)
            {
                Button btn1 = (Button)pnlButton.Controls[i];
            btn1.BackColor = Color.FromArgb(56, 56, 56);
            }
          
        }
        btn.BackColor = Color.FromArgb(96, 96, 96);
        int ma = int.Parse(btn.ID);
        
        getData(ma);
    //    btn.BackColor = Color.Blue;


    }

    public void getData( int ma)
    {
        LinQ_RaoVatDataContext linQ = new LinQ_RaoVatDataContext();
        var query = from tinraovat in linQ.TIN_RAO_VATs
                    where tinraovat.MaDanhMucCon == ma && tinraovat.MaLoaiTinRaoVat == CMyStaticFunction.layMaTinRaoVatHot()
                    && tinraovat.Deleted == false 
                    select new
        {
            tinraovat.TenTinRaoVat,
            tinraovat.SoLanXem,
            tinraovat.MaTinRaoVat,
            tinraovat.ThoiGianDang
        };
        gridVIP.DataSource = query.OrderByDescending(p => p.ThoiGianDang).Take(5);
        
        gridVIP.DataBind();

        query = from tinraovat in linQ.TIN_RAO_VATs
                where tinraovat.MaDanhMucCon == ma && tinraovat.MaLoaiTinRaoVat == CMyStaticFunction.layMaTinRaoVatThuong()
                && tinraovat.Deleted == false 
                select new
                {
                    tinraovat.TenTinRaoVat,
                    tinraovat.SoLanXem,
                    tinraovat.MaTinRaoVat,
                    tinraovat.ThoiGianDang
                };
       
       gridNormal.DataSource = query.OrderByDescending(p => p.ThoiGianDang).Take(5);
        gridNormal.DataBind();
    }
  
}


public class DataGridTemplate : ITemplate
{
    ListItemType templateType;
    string columnName;

    public DataGridTemplate(ListItemType type, string colname)
    {
        templateType = type;
        columnName = colname;
    }

    public void InstantiateIn(System.Web.UI.Control container)
    {
        HyperLink lc = new HyperLink();
        switch (templateType)
        {
            case ListItemType.Header:
                lc.Text = "<B>" + columnName + "</B>";
                container.Controls.Add(lc);
                break;
            case ListItemType.Item:
                lc.Text = "Item " + columnName;
                container.Controls.Add(lc);
                break;
            case ListItemType.EditItem:
                TextBox tb = new TextBox();
                tb.Text = "";
                container.Controls.Add(tb);
                break;
            case ListItemType.Footer:
                lc.Text = "<I>" + columnName + "</I>";
                container.Controls.Add(lc);
                break;
        }
    }
}
